@tool
extends EditorPlugin

var _panel: VBoxContainer
var _panel_button: Button = null

var _device_option: OptionButton
var _orientation_option: OptionButton
var _enabled_check: CheckBox
var _width_edit: LineEdit
var _height_edit: LineEdit
var _frame_path_edit: LineEdit

var _devices := {
	"Pixel 6 (1080x2400)": Vector2i(1080, 2400),
	"Pixel 6 Pro (872x1764)": Vector2i(872, 1764),
	"iPhone 13 (1170x2532)": Vector2i(1170, 2532),
	"iPone 16 (1179x2556)": Vector2i(1179, 2556),
	"iPad Air (1640x2360)": Vector2i(1640, 2360),
	"Custom": Vector2i(720, 1280)
}

func _enter_tree() -> void:
	# Ensure autoload exists when plugin is active.
	if not ProjectSettings.has_setting("autoload/MobileEmulator"):
		add_autoload_singleton("MobileEmulator", "res://addons/rotto_plugin/mobile_emulator.gd")

	# Build simple bottom panel UI.
	_panel = VBoxContainer.new()
	_panel.name = "Mobile Emulator"

	var hb1 := HBoxContainer.new()
	_enabled_check = CheckBox.new()
	_enabled_check.text = "启用模拟"
	_enabled_check.button_pressed = bool(ProjectSettings.get_setting("mobile_emulator/enabled", true))
	b1_add(hb1)

	var hb2 := HBoxContainer.new()
	var label := Label.new(); label.text = "设备"
	hb2.add_child(label)
	_device_option = OptionButton.new()
	for name in _devices.keys():
		_device_option.add_item(name)
	hb2.add_child(_device_option)
	_panel.add_child(hb2)

	var hb3 := HBoxContainer.new()
	var l2 := Label.new(); l2.text = "方向"
	hb3.add_child(l2)
	_orientation_option = OptionButton.new()
	_orientation_option.add_item("竖屏")
	_orientation_option.add_item("横屏")
	var ori := String(ProjectSettings.get_setting("mobile_emulator/orientation", "portrait"))
	_orientation_option.select(1 if ori == "landscape" else 0)
	hb3.add_child(_orientation_option)
	_panel.add_child(hb3)

	var hb4 := HBoxContainer.new()
	var l3 := Label.new(); l3.text = "分辨率"
	hb4.add_child(l3)
	_width_edit = LineEdit.new(); _width_edit.placeholder_text = "宽"
	_width_edit.text = str(ProjectSettings.get_setting("mobile_emulator/width", 1080))
	hb4.add_child(_width_edit)
	_height_edit = LineEdit.new(); _height_edit.placeholder_text = "高"
	_height_edit.text = str(ProjectSettings.get_setting("mobile_emulator/height", 2400))
	hb4.add_child(_height_edit)
	_panel.add_child(hb4)

	var hb5 := HBoxContainer.new()
	var l4 := Label.new(); l4.text = "外壳图片路径"
	hb5.add_child(l4)
	_frame_path_edit = LineEdit.new()
	# 固定使用横屏外壳图片
	var default_frame_path := "res://addons/rotto_plugin/frames/frame-horizontal.png"
	ProjectSettings.set_setting("mobile_emulator/frame_texture_path", default_frame_path)
	ProjectSettings.save()
	_frame_path_edit.text = default_frame_path
	hb5.add_child(_frame_path_edit)
	_panel.add_child(hb5)

	# 根据外壳图片尺寸自动更新分辨率编辑框与设置
	if ResourceLoader.exists(default_frame_path):
		var tex: Texture2D = load(default_frame_path)
		if tex != null:
			var s := Vector2i(tex.get_size())
			_width_edit.text = str(s.x)
			_height_edit.text = str(s.y)
			ProjectSettings.set_setting("mobile_emulator/width", s.x)
			ProjectSettings.set_setting("mobile_emulator/height", s.y)
			ProjectSettings.save()

	var apply_btn := Button.new(); apply_btn.text = "应用"
	apply_btn.pressed.connect(_on_apply_pressed)
	_panel.add_child(apply_btn)

	_panel_button = add_control_to_bottom_panel(_panel, "Mobile Emulator")

func b1_add(hb1: HBoxContainer) -> void:
	hb1.add_child(_enabled_check)
	_panel.add_child(hb1)

func _exit_tree() -> void:
	if _panel_button != null:
		remove_control_from_bottom_panel(_panel)
		_panel_button = null

func _enable_plugin() -> void:
	if not ProjectSettings.has_setting("autoload/MobileEmulator"):
		add_autoload_singleton("MobileEmulator", "res://addons/rotto_plugin/mobile_emulator.gd")

func _disable_plugin() -> void:
	if ProjectSettings.has_setting("autoload/MobileEmulator"):
		remove_autoload_singleton("MobileEmulator")

func _on_apply_pressed() -> void:
	# Device preset -> fill resolution edits if not custom.
	var idx := _device_option.get_selected()
	var name := _device_option.get_item_text(idx)
	var preset := _devices.get(name, Vector2i(int(_width_edit.text), int(_height_edit.text)))
	if name != "Custom":
		_width_edit.text = str(preset.x)
		_height_edit.text = str(preset.y)

	ProjectSettings.set_setting("mobile_emulator/enabled", _enabled_check.button_pressed)
	ProjectSettings.set_setting("mobile_emulator/width", int(_width_edit.text))
	ProjectSettings.set_setting("mobile_emulator/height", int(_height_edit.text))
	var ori := "landscape" if _orientation_option.get_selected() == 1 else "portrait"
	ProjectSettings.set_setting("mobile_emulator/orientation", ori)
	# 强制写入固定为横屏外壳路径
	var fixed_path := "res://addons/rotto_plugin/frames/frame-horizontal.png"
	ProjectSettings.set_setting("mobile_emulator/frame_texture_path", fixed_path)
	# 如果存在该图片，则用图片尺寸覆写宽高
	if ResourceLoader.exists(fixed_path):
		var tex: Texture2D = load(fixed_path)
		if tex != null:
			var s := Vector2i(tex.get_size())
			ProjectSettings.set_setting("mobile_emulator/width", s.x)
			ProjectSettings.set_setting("mobile_emulator/height", s.y)
			_width_edit.text = str(s.x)
			_height_edit.text = str(s.y)
	ProjectSettings.save()
